﻿using System;
using System.Collections.Generic;
using System.Reflection;
namespace CoreHelper
{
    /// <summary>
    /// 扩展方法
    /// </summary>
    public static partial class ExtensionMethod
    {
        /// <summary>
        /// bool转换
        /// </summary>
        /// <param name="b">bool值</param>
        /// <param name="trueText">trueText</param>
        /// <param name="falseText">falseText</param>
        /// <returns></returns>
        public static string ToText(this bool b, string trueText, string falseText)
        {
            return b ? trueText : falseText;
        }
        /// <summary>
        /// bool转换
        /// </summary>
        /// <param name="b">bool值</param>
        /// <returns></returns>
        public static string ToSex(this bool b)
        {
            return b ? "男" : "女";
        }

        /// <summary>
        /// bool转换
        /// </summary>
        /// <param name="b">bool值</param>
        /// <returns></returns>
        public static string ToShow(this bool b)
        {
            return b ? "显示" : "不显示";
        }

        /// <summary>
        /// bool转换
        /// </summary>
        /// <param name="b">bool值</param>
        /// <returns></returns>
        public static string ToPublic(this bool b)
        {
            return b ? "公开" : "保密";
        }

        /// <summary>
        /// bool转换
        /// </summary>
        /// <param name="b">bool值</param>
        /// <returns></returns>
        public static string ToEnable(this bool b)
        {
            return b ? "启用" : "禁用";
        }

        /// <summary>
        /// bool转换
        /// </summary>
        /// <param name="b">bool值</param>
        /// <returns></returns>
        public static string ToLock(this bool b)
        {
            return b ? "锁定" : "正常";
        }

        /// <summary>
        /// bool转换
        /// </summary>
        /// <param name="b">bool值</param>
        /// <returns></returns>
        public static string ToCheck(this bool b)
        {
            return b ? "审核过" : "未审核";
        }


        private static Dictionary<Enum, string> dictDiscs = new Dictionary<Enum, string>();
        /// <summary>
        /// 枚举转字典
        /// </summary>
        /// <param name="myEnum">枚举</param>
        /// <returns></returns>
		public static string Discription(this Enum myEnum)
        {
            string strDisc = string.Empty;

            if (dictDiscs.ContainsKey(myEnum))
            {
                strDisc = dictDiscs[myEnum];
            }
            else
            {
                strDisc = GetDiscription(myEnum);
                dictDiscs.Add(myEnum, strDisc);
            }
            return strDisc;
        }


        /// <summary>
        /// 获取枚举的描述
        /// </summary>
        /// <param name="myEnum">枚举</param>
        /// <returns></returns>
		private static string GetDiscription(Enum myEnum)
        {

            FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString());

            object[] attrs = fieldInfo.GetCustomAttributes(typeof(ItemDiscAttribute), true);

            if (attrs != null && attrs.Length > 0)
            {

                ItemDiscAttribute desc = attrs[0] as ItemDiscAttribute;

                if (desc != null)
                {

                    return desc.Description;

                }
            }
            return myEnum.ToString();

        }
    }
}